package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.ProductInfoMapper;
import com.cn.medicalcare.mapper.ProductMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Page;
import com.cn.medicalcare.pojo.Product;
import com.cn.medicalcare.pojo.ProductAllInfo;
import com.cn.medicalcare.service.ProductService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

public class ProductServiceImpl implements ProductService {

    public static final String PRODUCT_PAGE_CACHE_KEY = "product_page_by_cate_id_";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final ProductServiceImpl instance = new ProductServiceImpl();
    }

    private ProductServiceImpl() {
    }

    public static ProductServiceImpl getInstance() {
        return ProductServiceImpl.SingletonClassInstance.instance;
    }


    @Override
    public List<Product> selectProductInPage(AllPage<Product> allPage) {
        return SqlSessionFactoryTool.DoMapper(ProductMapper.class, m -> m.selectProductInPage(allPage));
    }

    @Override
    public int selectProductCount(Product product) {
        return SqlSessionFactoryTool.DoMapper(ProductMapper.class, m -> m.selectProductCount(product));
    }

    @Override
    public boolean insertProduct(Product product) {
        product.setOnshelfupdatetime(new Date());
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(ProductMapper.class, m -> m.insertProduct(product));
        if (insertFlag) {
            JedisUtil.deleteKeys(PRODUCT_PAGE_CACHE_KEY + product.getCategory_id() + "*");
        }
        return insertFlag;
    }

    @Override
    public boolean updateProduct(Product product) {
        if (product.getOnshelf() == 1) {
            product.setOnshelfupdatetime(new Date());
        }
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(ProductMapper.class, m -> m.updateProduct(product));
        String cachekey = ProductInfoServiceImpl.PRODUCT_CACHE_KEY + product.getId();
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(cachekey));
            JedisUtil.deleteKeys(PRODUCT_PAGE_CACHE_KEY + product.getCategory_id() + "*");
        }
        return updateFlag;
    }

    @Override
    public List<Product> selectProductByCateId(int cate_id, Page page) {
        List<Product> list = null;
        String cachekey = PRODUCT_PAGE_CACHE_KEY + cate_id + "_from_" + page.getSkip() + "_to_" + (page.getSkip() + page.getLimit());
        try {
            String productListJson = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(productListJson)) {
                JedisUtil.jedisDo(j -> j.expire(cachekey, JedisUtil.CACHE_TIME));
                list = JsonTool.toCollection(productListJson, new TypeReference<List<Product>>() {
                });
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(ProductMapper.class, m -> m.selectProductByCateId(cate_id, page));
            try {
                String productListJson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, productListJson));
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public int selectProductCountByCateId(int cate_id) {
        Integer icount = null;
        String cachekey = PRODUCT_PAGE_CACHE_KEY + cate_id;
        try {
            String count = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(count)) {
                icount = Integer.parseInt(count);
            }
        } catch (Exception e) {
        }
        if (icount == null) {
            icount = SqlSessionFactoryTool.DoMapper(ProductMapper.class, m -> m.selectProductCountByCateId(cate_id));
            String productCount = icount.toString();
            try {
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, productCount));
            } catch (Exception e) {
            }
        }
        return icount;
    }
}
